/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

/**
 *
 * @author MrDoanCoi
 */

import DAL.MyConnectUnit;
import DTO.Period;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;
public class BPeriod {
        MyConnectUnit connect;
     public BPeriod() {
        this.connect = DAL.DAL.getDAL();
    }
        public HashSet<Period>getPeriods(String condition, String OderBy) throws Exception{
        ResultSet result = this.connect.Select("Period",condition,OderBy);
        HashSet<Period> periods = new HashSet<Period>();
        while(result.next()){
            Period period = new Period();
            period.setPeriodID(result.getInt("PeriodID"));
            period.setPeriodName(result.getString("PeriodName"));
       
            
            periods.add(period);
        }
        return periods;
    }
          public HashSet<Period>getPeriods(String condition) throws Exception{
        return getPeriods(condition,null);
    }
  public HashSet<Period>getPeriods() throws Exception{
        return getPeriods(null);
    }
    
       public Period getByID(int id)throws Exception{
        HashSet<Period>periods = this.getPeriods("PeriodID = "+ id);
        if(periods.size()>0)
            return periods.toArray(new Period[periods.size()])[0];
        return null;
       
   }
         public void Insert(Period  period)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("PeriodID", period.getPeriodID());
        map.put("PeriodName", period.getPeriodName());
        
        this.connect.Insert("Period", map);
       
   }
         public void Inserts(HashSet<Period> periods)throws Exception{
        for(Period period:periods)
            this.Insert(period);
       
   }
          public void Update(Period period)throws Exception{
        HashMap<String, Object>map = new HashMap<String,Object>();
        map.put("PeriodName", period.getPeriodName());
        boolean Update = this.connect.Update("Period", map,"PeriodID="+period.getPeriodID());
       
   }
            public void Updates(HashSet<Period> periods)throws Exception{
        for(Period period:periods)
            this.Update(period);
}
      public void Delete(Period period) throws Exception{
        this.connect.Delete("Period", "PeriodID="+period.getPeriodID());
    }
       public void Deletes(HashSet<Period> periods) throws Exception {
        for(Period period:periods)
            this.Delete(period);
   }        
      
}
